﻿using System.Diagnostics.Contracts;

namespace Leek.Persistence
{
    public sealed class DomainObjectProperty<T>
    {
        private readonly IDomainObject _owner;
        private T _value;


        public T Value
        {
            [Pure]
            get { return _value; }
            set
            {
                if (_owner.State == DomainObjectState.PendingDeletion || _owner.State == DomainObjectState.Deleted)
                {
                    throw new DomainObjectDeletedException();
                }
                else if (!_value.Equals(value))
                {
                    _value = value;
                    if (_owner.State == DomainObjectState.Persisted)
                    {
                        _owner.State = DomainObjectState.Modified;
                    }
                }
            }
        }


        internal DomainObjectProperty(IDomainObject owner, T initialValue = default(T))
        {
            _value = initialValue;
            _owner = owner;
        }
    }
}
